<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-5-15
 * Time: 下午6:30
 */

namespace common\components\tools;


use common\models\ImageIndex;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;

class Word
{
    public static function setWordImageHeight($img, $h = 450){
        $w = 600;
        if (empty($img)) return null;
        $a = getimagesize($img);
        $s0 = $w/$h;
        $s1 = $a[0]/$a[1];
        if($s0>=$s1 && $a[1]>$h)
        {
            $d['height'] = $h;
            $d['width'] = $h/$a[1]*$a[0];
            return $d;
        }
        if($s0<$s1 && $a[0]>$w)
        {
            $d['width'] = $w;
            $d['height'] = $w/$a[0]*$a[1];
            return $d;
        }
        return null;
    }

    /**
     * 上传征信图片
     * @param $images
     */
    public static function createCreditWord($images)
    {
        $phpword = new PhpWord();
        $phpword_section = $phpword->createSection();
        $runtime_path = '@uploads_root/docx/credit/images';
//        $runtime_path = \Yii::getAlias('@uploads_root/docx/credit/images');
        $upload_path = \Yii::getAlias('@uploads_root/docx/credit');
        $upload_web = \Yii::getAlias('@uploads_url/docx/credit');
        if (is_dir($upload_path)){}else{
            FileHelper::createDirectory($upload_path);
        }
        $images = ArrayHelper::map($images, 'picid', 'picurl');
        if ($images['0001']){
            $url = $images['0001'];
            $m = ImageIndex::imageModelGet($url, $runtime_path);
            $z = $m->imageFilePath;
            $phpword_section->addImage($z, self::setWordImageHeight($z, 900));
        }else{
            throw new ErrorException('图片上传pic错误', 10511);
        }
        $phpword_section->addPageBreak();
        if ($images['0002']){
            $url = $images['0002'];
            $m = ImageIndex::imageModelGet($url, $runtime_path);
            $z = $m->imageFilePath;
            $phpword_section->addImage($z, self::setWordImageHeight($z, 450));
        }else{
            throw new ErrorException('图片上传pic错误', 10512);
        }
        if ($images['0003']){
            $url = $images['0003'];
            $m = ImageIndex::imageModelGet($url, $runtime_path);
            $z = $m->imageFilePath;
            $phpword_section->addImage($z, self::setWordImageHeight($z, 450));
        }else{
            throw new ErrorException('图片上传pic错误', 10513);
        }
        $phpword_section->addPageBreak();
        $objWriter = IOFactory::createWriter($phpword, 'Word2007');
        $name = time().rand(1000, 9999).".docx";
        $doc_root = $upload_path .'/'.$name;
        $doc_web = $upload_web .'/'.$name;
        $objWriter->save( $doc_root );
        return $doc_web;
    }

    /**
     * php获取文件扩展名
     * @param $url
     * @return mixed
     */
    public static function getExt($url)
    {
        return substr($url, strrpos($url, '.')+1);
    }
}